<?php
class ControllerAccountResetpass extends Controller {
	private $error = array();

	public function index() {
		if ($this->customer->isLogged()) {
			$this->redirect($this->url->link('account/account', '', 'SSL'));
		}
        $array = explode('|',base64_decode($_GET['p']));

        if(!$array){
            $this->redirect($this->url->link('account/account', '', 'SSL'));
        }

		$this->language->load('account/resetpass');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('account/customer');

        $customer=$this->model_account_customer->getCustomerByEmail($array['0']);

        $checkCode = md5($array['0'].'+'.$customer['password']);

        if($array['1'] != $checkCode ){
            $this->redirect($this->url->link('account/account', '', 'SSL'));
        }


        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->load->model('account/customer');

            if ($customer['status']!=1) {
                //同步到后台
                $this->backend->register_user($customer['email'], $customer['password_plain']);
                $this->backend->recharge($customer['email'],$customer['password_plain'],10);
                $this->model_account_customer->activateCustomerByemail($customer['email']);

                $this->session->data['success'] = $this->language->get('text_success');
            }else{
                $this->load->model('account/customer');
                $this->model_account_customer->editPassword($array['0'], $this->request->post['password']);
                $this->session->data['success'] = $this->language->get('text_success');
            }
            $this->redirect($this->url->link('account/login', '', 'SSL'));

        }

		$this->data['breadcrumbs'] = array();

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),        	
			'separator' => false
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_account'),
			'href'      => $this->url->link('account/account', '', 'SSL'),     	
			'separator' => $this->language->get('text_separator')
		);

		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_resetpass'),
			'href'      => $this->url->link('account/resetpass', '', 'SSL'),
			'separator' => $this->language->get('text_separator')
		);

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['entry_password'] = $this->language->get('entry_password');
		$this->data['entry_confirm'] = $this->language->get('entry_confirm');

		$this->data['button_continue'] = $this->language->get('button_continue');
		$this->data['button_back'] = $this->language->get('button_back');

		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

        if (isset($this->error['password'])) {
            $this->data['error_password'] = $this->error['password'];
        } else {
            $this->data['error_password'] = '';
        }

        if (isset($this->error['confirm'])) {
            $this->data['error_confirm'] = $this->error['confirm'];
        } else {
            $this->data['error_confirm'] = '';
        }

        if (isset($this->request->post['password'])) {
            $this->data['password'] = $this->request->post['password'];
        } else {
            $this->data['password'] = '';
        }

        if (isset($this->request->post['confirm'])) {
            $this->data['confirm'] = $this->request->post['confirm'];
        } else {
            $this->data['confirm'] = '';
        }


		$this->data['action'] = $this->url->link('account/resetpass', 'p='.$_GET['p'].'', 'SSL');

		$this->data['back'] = $this->url->link('account/login', '', 'SSL');

		if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/account/resetpass.tpl')) {
			$this->template = $this->config->get('config_template') . '/template/account/resetpass.tpl';
		} else {
			$this->template = 'default/template/account/resetpass.tpl';
		}

		$this->children = array(
			'common/column_left',
			'common/column_right',
			'common/content_top',
			'common/content_bottom',
			'common/footer',
			'common/header'	
		);

		$this->response->setOutput($this->render());		
	}

    protected function validate() {
        if ((utf8_strlen($this->request->post['password']) < 6) || (utf8_strlen($this->request->post['password']) > 20)) {
            $this->error['password'] = $this->language->get('error_password');
        }

        if ($this->request->post['confirm'] != $this->request->post['password']) {
            $this->error['confirm'] = $this->language->get('error_confirm');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }
}
?>